package net.telesing.tcmc.back.dao.user.impl;

import net.telesing.tcmc.back.dao.user.UGeneralDao;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.FieldFilter;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.trans.Atom;

import java.util.List;

/**
 * Created by 一凡 on 2015/11/3.
 */
@IocBean
public class UserDaoImpl implements UGeneralDao {
    @Inject
    private Dao dao;

    @Override
    public <T> T insert(T obj) {
        return dao.insert(obj);
    }

    @Override
    public <T> int modified(final T obj) {
        final int[] s = {0};
        FieldFilter.create(obj.getClass(), null, "sunyifan", false).run(new Atom() {
            @Override
            public void run() {
                s[0] = dao.update(obj);
            }
        });
        return s[0];
    }

    @Override
    public <T> int delete(Class<T> tClass, Long id) {
        return dao.delete(tClass, id);
    }

    @Override
    public <T> int delete(Class<T> tClass) {
        return dao.delete(tClass);
    }

    @Override
    public <T> T fetch(Class<T> tClass, Condition condition) {
        return dao.fetch(tClass, condition);
    }

    @Override
    public <T> T fetch(Class<T> tClass, String sql) {
        Sql sqls = Sqls.create(sql);
        sqls.setCallback(Sqls.callback.entities());
        sqls.setEntity(dao.getEntity(tClass));
        dao.execute(sqls);
        return sqls.getObject(tClass);
    }

    @Override
    public <T> T fetch(Class<T> tClass, Long id) {
        return dao.fetch(tClass, id);
    }

    @Override
    public <T> List<T> query(Class<T> tClass, Condition condition) {
        return dao.query(tClass, condition);
    }

    @Override
    public <T> List<T> query(Class<T> tClass, String sql) {
        Sql sqls = Sqls.create(sql);
        sqls.setCallback(Sqls.callback.entities());
        sqls.setEntity(dao.getEntity(tClass));
        dao.execute(sqls);
        return sqls.getList(tClass);
    }

}
